package com.reader.base;

import java.util.ArrayList;
import java.util.regex.Pattern;

public class StringTool {

    /**
     * strings to Hexadecimal array,seperate string by space
     *
     * @param strHexValue Hexadecimal string
     * @return byte array
     */
    public static byte[] stringToByteArray(String strHexValue) {
        String[] strAryHex = strHexValue.split(" ");
        byte[] btAryHex = new byte[strAryHex.length];

        try {
            int nIndex = 0;
            for (String strTemp : strAryHex) {
                btAryHex[nIndex] = (byte) Integer.parseInt(strTemp, 16);
                nIndex++;
            }
        } catch (NumberFormatException e) {

        }

        return btAryHex;
    }

    /**
     * string array to Hexadecimal array
     *
     * @param strAryHex string array needs to be transfered
     * @param nLen      length
     * @return byte array
     */
    public static byte[] stringArrayToByteArray(String[] strAryHex, int nLen) {
        if (strAryHex == null) return null;

        if (strAryHex.length < nLen) {
            nLen = strAryHex.length;
        }

        byte[] btAryHex = new byte[nLen];

        try {
            for (int i = 0; i < nLen; i++) {
                btAryHex[i] = (byte) Integer.parseInt(strAryHex[i], 16);
            }
        } catch (NumberFormatException e) {

        }

        return btAryHex;
    }

    /**
     * Hexadecimal character array to strings
     *
     * @param btAryHex string array needs to be transfered
     * @param nIndex   start position
     * @param nLen     transfered length
     * @return transfered strings
     */
    public static String byteArrayToString(byte[] btAryHex, int nIndex, int nLen) {
        if (nIndex + nLen > btAryHex.length) {
            nLen = btAryHex.length - nIndex;
        }

        String strResult = String.format("%02X", btAryHex[nIndex]);
        for (int nloop = nIndex + 1; nloop < nIndex + nLen; nloop++) {
            String strTemp = String.format(" %02X", btAryHex[nloop]);

            strResult += strTemp;
        }

        return strResult;
    }

    /**
     * intercept string to the specified length and shift into character array
     *
     * @param strValue input strings
     * @return byte array
     */
    public static String[] stringToStringArray(String strValue, int nLen) {
        String[] strAryResult = null;

        if (strValue != null && !strValue.equals("")) {
            ArrayList<String> strListResult = new ArrayList<String>();
            String strTemp = "";
            int nTemp = 0;

            for (int nloop = 0; nloop < strValue.length(); nloop++) {
                if (strValue.charAt(nloop) == ' ') {
                    continue;
                } else {
                    nTemp++;

                    if (!Pattern.compile("^(([A-F])*([a-f])*(\\d)*)$")
                            .matcher(strValue.substring(nloop, nloop + 1))
                            .matches()) {
                        return strAryResult;
                    }

                    strTemp += strValue.substring(nloop, nloop + 1);

                    if ((nTemp == nLen) || (nloop == strValue.length() - 1
                            && (strTemp != null && !strTemp.equals("")))) {
                        strListResult.add(strTemp);
                        nTemp = 0;
                        strTemp = "";
                    }
                }
            }
            if (strListResult.size() > 0) {
                strAryResult = new String[strListResult.size()];
                for (int i = 0; i < strAryResult.length; i++) {
                    strAryResult[i] = strListResult.get(i);
                }
            }
        }
        return strAryResult;
    }

    /**
     * 16进制字符数组转成字符串。
     *
     * @param btAryHex 要转换的字符串数组
     *                 //@param nIndex	起始位置
     *                 //@param nLen		长度
     * @return 字符串
     */
    public static String byteArrayToStrData(byte[] btAryHex/*, int nIndex, int nLen*/) {
        //inventoryBuffer 情况
        //15
        //3, 6
        //9，6
        //读tag情况
        //17
        //5，16
        int nIndex;
        int nLen = 6;
        //截取前12位，开头为86
//        StringBuilder sb = new StringBuilder();
//        for (int i = 0; i < 17; i++) {
//            sb.append(String.format("%02X ", btAryHex[i]));
//        }
//		String sStr = String.format("%02X", btAryHex[3]);
        String sStr = String.format("%02X", btAryHex[5]);
        if (sStr.equals("86")) {
            //截取前12位
            //nIndex = 3;
            nIndex = 5;
        } else {
            //截取后12位
            //nIndex = 9;
            nIndex = 11;
        }

        StringBuilder strResult = new StringBuilder(String.format("%02X", btAryHex[nIndex]));
        for (int nloop = nIndex + 1; nloop < nIndex + nLen; nloop++) {
            String strTemp = String.format(" %02X", btAryHex[nloop]);
            strResult.append(strTemp);
        }

        return strResult.toString();
    }
}
